package com.ajk.server.dao.provider;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;

import com.ajk.server.model.TestAnswer;
import com.ajk.server.model.query.TestAnswerQuery;
import com.ajk.server.utils.IntegerUtil;
/**
 * 测试记录  <br>
 * t_test_answer <br>
 * v1.0 2016-05-01
 * @see com.ajk.server.dao.TestAnswerMapper
 */
public class TestAnswerProvider {

	/**
	 * 新增 测试记录
	 */
	public String insert(TestAnswer domain){
		StringBuilder sb = new StringBuilder();

		sb.append("insert into t_test_answer(create_time,last_modify_time");
		if(domain.getCategoryId()!=null){
			sb.append(",category_id");
		}
		if(domain.getScore()!=null){
			sb.append(",score");
		}
		if(domain.getUserId()!=null){
			sb.append(",user_id");
		}
		if(domain.getInUsed()!=null){
			sb.append(",in_used");
		}
		if(domain.getResult()!=null){
			sb.append(",result");
		}
		if(domain.getResultDesc()!=null){
			sb.append(",result_desc");
		}
		if(domain.getDoctorSuggest()!=null){
			sb.append(",doctor_suggest");
		}
		if(domain.getStartTime()!=null){
			sb.append(",start_time");
		}
		if(domain.getEndTime()!=null){
			sb.append(",end_time");
		}
		if(domain.getStationId()!=null){
			sb.append(",station_id");
		}

		sb.append(") values ( now(),now() ");

		if(domain.getCategoryId()!=null){
			sb.append(",#{categoryId}");
		}
		if(domain.getScore()!=null){
			sb.append(",#{score}");
		}
		if(domain.getUserId()!=null){
			sb.append(",#{userId}");
		}
		if(domain.getInUsed()!=null){
			sb.append(",#{inUsed}");
		}
		if(domain.getResult()!=null){
			sb.append(",#{result}");
		}
		if(domain.getResultDesc()!=null){
			sb.append(",#{resultDesc}");
		}
		if(domain.getDoctorSuggest()!=null){
			sb.append(",#{doctorSuggest}");
		}
		if(domain.getStartTime()!=null){
			sb.append(",#{startTime}");
		}
		if(domain.getEndTime()!=null){
			sb.append(",#{endTime}");
		}
		if(domain.getStationId()!=null){
			sb.append(",#{stationId}");
		}
		sb.append(")");

		return sb.toString();
	}

	/**
	 * 更新 测试记录
	 */
	public String update(TestAnswer domain){
		StringBuilder sb = new StringBuilder();

		sb.append("update t_test_answer set last_modify_time = now() ");

		if(domain.getCategoryId()!=null){
			sb.append(",category_id=#{categoryId}");
		}
		if(domain.getScore()!=null){
			sb.append(",score=#{score}");
		}
		if(domain.getUserId()!=null){
			sb.append(",user_id=#{userId}");
		}
		if(domain.getInUsed()!=null){
			sb.append(",in_used=#{inUsed}");
		}
		if(domain.getResult()!=null){
			sb.append(",result=#{result}");
		}
		if(domain.getResultDesc()!=null){
			sb.append(",result_desc=#{resultDesc}");
		}
		if(domain.getDoctorSuggest()!=null){
			sb.append(",doctor_suggest=#{doctorSuggest}");
		}
		if(domain.getStartTime()!=null){
			sb.append(",start_time=#{startTime}");
		}
		if(domain.getEndTime()!=null){
			sb.append(",end_time=#{endTime}");
		}

		sb.append(" where id=#{id}");

		return sb.toString();
	}

	public String batchInsert(Map<String, List<TestAnswer>> map){
		StringBuffer sb = new StringBuffer();
		List<TestAnswer> testAnswers = map.get("list");
		sb.append("insert into t_test_answer(create_time,last_modify_time,category_id,score,user_id,in_used,result,result_desc,doctor_suggest,start_time,end_time) values");
		MessageFormat messageFormat = new MessageFormat("("
				+ "now(),now()"
				+ ",#'{'list[{0}].categoryId}"
				+ ",#'{'list[{0}].score}"
				+ ",#'{'list[{0}].userId}"
				+ ",#'{'list[{0}].inUsed}"
				+ ",#'{'list[{0}].result}"
				+ ",#'{'list[{0}].resultDesc}"
				+ ",#'{'list[{0}].doctorSuggest}"
				+ ",#'{'list[{0}].startTime}"
				+ ",#'{'list[{0}].endTime}"
				+")");

		if(CollectionUtils.isNotEmpty(testAnswers)) {
			sb.append(messageFormat.format(new Integer[]{0}));
			for (int i = 1; i <  testAnswers.size(); i++) {
				sb.append(",").append(messageFormat.format(new Integer[]{i}));
			}
		}
		return sb.toString();
	}

	/**
	 *通过query 查询 测试记录
	 */
	public String findByQuery(TestAnswerQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select * from t_test_answer where in_used=1 ");

		if(IntegerUtil.isPositiveInteger(query.getUserId())){
			sb.append(" and user_id=#{userId}");
		}

		if (CollectionUtils.isNotEmpty(query.getCategoryIdList())) {
			sb.append(" and category_id in (#{categoryIdList[0]}");
			for (int i = 1; i < query.getCategoryIdList().size(); i++) {
				sb.append(",#{categoryIdList[").append(i).append("]}");
			}
			sb.append(")");
		}

		if(IntegerUtil.isPositiveInteger(query.getStartId())){
			sb.append(" and id>#{startId} order by id limit #{limit}");
		}else if(IntegerUtil.isPositiveInteger(query.getEndId())){
			sb.append(" and id<#{endId} order by id desc limit #{limit}");
		}else if(query.getStart()!=null && query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" order by id desc limit #{start},#{limit}");
		}else if(query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" order by id desc limit #{limit}");
		}

		return sb.toString();
	}

	public String countByQuery(TestAnswerQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select count(1) from t_test_answer where in_used=1 ");

		if(IntegerUtil.isPositiveInteger(query.getUserId())){
			sb.append(" and user_id=#{userId}");
		}

		if (CollectionUtils.isNotEmpty(query.getCategoryIdList())) {
			sb.append(" and category_id in (#{categoryIdList[0]}");
			for (int i = 1; i < query.getCategoryIdList().size(); i++) {
				sb.append(",#{categoryIdList[").append(i).append("]}");
			}
			sb.append(")");
		}

		return sb.toString();
	}

	/**
	 *通过ids 查询 测试记录
	 */
	public String findByIds(Map<String, List<Integer>> map){
		StringBuffer sb = new StringBuffer();
		List<Integer> ids = map.get("list");
		sb.append("select * from t_test_answer where ");
		if(ids==null || ids.isEmpty()){
			sb.append(" 1<2");
		}else{
			if(ids.size()==1){
				sb.append(" id=").append("#{list[0]}");
			}else{
				sb.append(" id in (").append("#{list[0]}");
				for(int i=1; i<ids.size() ; i++){
					sb.append(",").append("#{list[").append(i).append("]}");
				}
				sb.append(")");
			}
		}
		return sb.toString();
	}

	public String findAnswerByUserIdParentId(Map<String, Integer> map){
		Integer start = map.get("start");
		Integer limit = map.get("limit");
		StringBuilder sb = new StringBuilder();
		sb.append("select a.*,b.name paper_name from t_test_answer a,dict_test_category b where a.category_id=b.id and "
				+ " a.user_id=#{userId}");
		sb.append(" order by create_time desc ");
		if(start!=null && IntegerUtil.isPositiveInteger(limit)){
			sb.append(" limit #{start},#{limit}");
		}else if(IntegerUtil.isPositiveInteger(limit)){
			sb.append(" limit #{limit}");
		}
		return sb.toString();
	}
}	